home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
system
/
csh4.zip
/
CAT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-09-04
|
1KB
|
72 lines
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
void (*signal())();
FILE *fd1,*fd2;
void (*oldsig)();
jmp_buf catenv;
catintr()
{
signal(SIGINT,oldsig); /* restore shell interrupt */
fclose(fd1);
fclose(fd2);
longjmp(catenv,-1);
}
cat(argc,argv)
char *argv[];
{
char *intmsg = "Interrupt received\n";
FILE *fdopen(), *fopen();
if (-1==setjmp(catenv))
{
write(2,intmsg,strlen(intmsg));
return -1;
}
oldsig = signal(SIGINT,catintr); /* trap interrupts from keyboard */
/* get standard output opened for business */
if (NULL == (fd2 = fdopen(1,"w")))
{
perror("cat : Can't open stdout");
}
/* handle pipes */
if (argc == 1)
{
if (NULL == (fd1 = fdopen(0,"r")))
{
perror("cat : Can't open stdin");
}
_cat();
fclose(fd1);fclose(fd2);
}
/* handle specified files */
else
{
while(--argc)
{
if (NULL == (fd1 = fopen(*(++argv),"r")))
{
fprintf(stderr,"can't open %s\n",*argv);
continue;
}
_cat();
fclose(fd1);
}
}
fclose(fd2);
signal(SIGINT,oldsig); /* restore old int catcher */
}
_cat()
{
char buffer[512];
while (NULL != fgets(buffer,512,fd1))
fputs(buffer,fd2);
}